package leetcode.editor.cn.q1_300.q150;

import java.util.ArrayList;
import java.util.List;

/**
 * @author qidi
 * @date 2022-01-21 09:36:26
 */
class Q0155_MinStack {

    //leetcode submit region begin(Prohibit modification and deletion)
    class MinStack {

        List<Integer> list;
        List<Integer> mins;
        int size;

        public MinStack() {
            list = new ArrayList<>();
            mins = new ArrayList<>();
            size = 0;
        }

        public void push(int val) {
            list.add(val);
            if (size == 0) {
                mins.add(0);
            } else {
                Integer preMin = mins.get(size - 1);
                mins.add(list.get(preMin) > val ? size : preMin);
            }
            size++;
        }

        public void pop() {
            size--;
            list.remove(size);
            mins.remove(size);
        }

        public int top() {
            return list.get(size - 1);
        }

        public int getMin() {
            return list.get(mins.get(size - 1));
        }
    }

    //leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        MinStack minStack = new Q0155_MinStack().new MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        System.out.println(minStack.getMin());
        minStack.pop();
        System.out.println(minStack.top());
        System.out.println(minStack.getMin());
    }
}